package com.google.android.apps.mytracks.investigados;

import com.google.android.apps.mytracks.content.Investigado;
import com.google.android.apps.mytracks.content.InvestigadosColumns;
import com.google.android.apps.mytracks.content.MyTracksProvider.DatabaseHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class InvestigadoDataSource {

  // Database fields
  private SQLiteDatabase database;
  private DatabaseHelper dbHelper;
  private String[] allColumns = InvestigadosColumns.COLUMNS;

  public InvestigadoDataSource(Context context) {
    dbHelper = new DatabaseHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public long createInvestigado(Investigado investigado) {
    ContentValues values = new ContentValues();
    values.put(InvestigadosColumns.NOMBRE, investigado.getNombre());
    values.put(InvestigadosColumns.DIRECCION, investigado.getDireccion());
    values.put(InvestigadosColumns.EMAIL, investigado.getEmail());
    long insertId = database.insert(InvestigadosColumns.TABLE_NAME, null, values);
    return insertId;
  }
  
  public Investigado getInvestigado(long investigadoId) {
    String[] args = new String[] {String.valueOf(investigadoId)};
    Cursor cursor = database.query(InvestigadosColumns.TABLE_NAME,
        allColumns, InvestigadosColumns._ID+"=?", args, null, null, null);
    cursor.moveToFirst();
    Investigado investigado = cursorToInvestigado(cursor);
    cursor.close();
    return investigado;
  }

  public void deleteInvestigado(Investigado investigado) {
    long id = investigado.getId();
    database.delete(InvestigadosColumns.TABLE_NAME, InvestigadosColumns._ID
        + " = " + id, null);
  }
  
  public void updateInvestigado(Investigado investigado) {
    
    long id = investigado.getId();
    ContentValues values = new ContentValues();
    values.put(InvestigadosColumns.NOMBRE, investigado.getNombre());
    values.put(InvestigadosColumns.DIRECCION, investigado.getDireccion());
    values.put(InvestigadosColumns.EMAIL, investigado.getEmail());
    String strFilter = "_id=" + id;
    database.update(InvestigadosColumns.TABLE_NAME, values, strFilter, null);
  }
  
  public void addCasoInvestigado(long investigadoId, long casoId) {
    
    ContentValues values = new ContentValues();
    values.put(InvestigadosColumns.CASOID, casoId);
    String strFilter = "_id=" + investigadoId;
    database.update(InvestigadosColumns.TABLE_NAME, values, strFilter, null);
  }

  public List<Investigado> getAllinvestigados() {
    List<Investigado> investigados = new ArrayList<Investigado>();

    Cursor cursor = database.query(InvestigadosColumns.TABLE_NAME,
        allColumns, null, null, null, null, InvestigadosColumns.DEFAULT_SORT_ORDER);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Investigado investigado = cursorToInvestigado(cursor);
      investigados.add(investigado);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return investigados;
  }

  private Investigado cursorToInvestigado(Cursor cursor) {
    Investigado investigado = new Investigado();
    investigado.setId(cursor.getLong(0));
    investigado.setNombre(cursor.getString(1));
    investigado.setDireccion(cursor.getString(2));
    investigado.setEmail(cursor.getString(3));
    return investigado;
  }
  
  public List<Investigado> getAllInvestigadosCaso(long casoId) {
    ArrayList<Investigado> investigados = new ArrayList<Investigado>();
    String[] args = new String[] {String.valueOf(casoId)};
    Cursor cursor = database.query(InvestigadosColumns.TABLE_NAME,
        allColumns, "casoId=?", args, null, null, InvestigadosColumns.DEFAULT_SORT_ORDER);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Investigado investigado = cursorToInvestigado(cursor);
      investigados.add(investigado);
      cursor.moveToNext();
    }
    cursor.close();
    return investigados;
  }
} 